<template>
  <div class="register-container">
    <el-row>
      <el-col :lg="14" :md="11" :sm="24" :xl="14" :xs="24">
        <div style="color: transparent">占位符</div>
      </el-col>
      <el-col :lg="9" :md="12" :sm="24" :xl="9" :xs="24">
        <el-form
          ref="registerForm"
          class="register-form"
          :model="form"
          :rules="registerRules"
          size="mini"
        >
          <div class="title-tips">{{ translateTitle('注册') }}</div>
          <el-form-item prop="user_name">
            <el-input
              v-model.trim="form.user_name"
              auto-complete="off"
              :placeholder="translateTitle('请输入用户名')"
              type="text"
            >
              <template #prefix>
                <vab-icon icon="user-line" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="user_mobile">
            <el-input
              v-model.trim="form.user_mobile"
              maxlength="11"
              :placeholder="translateTitle('请输入手机号')"
              show-word-limit
              type="text"
            >
              <template #prefix>
                <vab-icon icon="smartphone-line" />
              </template>
            </el-input>
          </el-form-item>
          <!-- <el-form-item prop="phoneCode" style="position: relative">
            <el-input
              v-model.trim="form.phoneCode"
              :placeholder="translateTitle('请输入手机验证码')"
              type="text"
            >
              <template #prefix>
                <vab-icon icon="barcode-box-line" />
              </template>
            </el-input>
            <el-button
              class="phone-code"
              :disabled="isGetPhone"
              type="primary"
              @click="getPhoneCode"
            >
              {{ phoneCode }}
            </el-button>
          </el-form-item> -->
          <el-form-item prop="user_password">
            <el-input
              v-model.trim="form.user_password"
              autocomplete="new-password"
              :placeholder="translateTitle('请输入密码')"
              type="password"
            >
              <template #prefix>
                <vab-icon icon="lock-line" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="user_password2">
            <el-input
              v-model.trim="form.user_password2"
              autocomplete="new-password2"
              :placeholder="translateTitle('请确认输入密码')"
              type="password"
            >
              <template #prefix>
                <vab-icon icon="lock-line" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <div class="captcha">
              <el-input
                v-model.trim="form.captcha"
                placeholder="验证码"
                type="text"
                @keyup.enter.native="handleRegister"
              >
                <template #prefix>
                  <vab-icon icon="shield-keyhole-line" />
                </template>
              </el-input>
              <div class="captchaImg" @click="getCaptcha">
                <img :src="captcha" />
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              class="register-btn"
              :loading="loading"
              type="primary"
              @click.native.prevent="handleRegister"
            >
              {{ translateTitle('注 册') }}
            </el-button>
            <div class="loginbot">
              <router-link to="/login">
                {{ translateTitle('登录') }}
              </router-link>
            </div>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :lg="1" :md="1" :sm="24" :xl="1" :xs="24">
        <div style="color: transparent">占位符</div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { translateTitle } from '@/utils/i18n'
import { isPassword, isPhone } from '@/utils/validate'
import { register, captcha } from '@/api/user'

export default {
  name: 'Register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if ('' === value) {
        callback(new Error(this.translateTitle('用户名不能为空')))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!isPassword(value)) {
        callback(new Error(this.translateTitle('密码不能少于6位')))
      } else {
        callback()
      }
    }
    const validatePassword2 = (rule, value, callback) => {
      if (this.form.user_password != value) {
        callback(new Error(this.translateTitle('两次密码不一致')))
      } else {
        callback()
      }
    }
    const validatePhone = (rule, value, callback) => {
      if (!isPhone(value)) {
        callback(new Error(this.translateTitle('请输入正确的手机号')))
      } else {
        callback()
      }
    }
    return {
      isGetPhone: false,
      timertimer: null,
      phoneCode: this.translateTitle('获取验证码'),
      form: {},
      registerRules: {
        user_name: [
          {
            required: true,
            trigger: 'blur',
            message: this.translateTitle('请输入用户名'),
          },
          {
            validator: validateUsername,
            trigger: 'blur',
          },
        ],
        user_mobile: [
          {
            required: true,
            trigger: 'blur',
            message: this.translateTitle('请输入手机号'),
          },
          {
            validator: validatePhone,
            trigger: 'blur',
          },
        ],
        user_password: [
          {
            required: true,
            trigger: 'blur',
            message: this.translateTitle('请输入密码'),
          },
          {
            validator: validatePassword,
            trigger: 'blur',
          },
        ],
        user_password2: [
          {
            required: true,
            trigger: 'blur',
            message: this.translateTitle('请确认密码'),
          },
          {
            validator: validatePassword2,
            trigger: 'blur',
          },
        ],
        captcha: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入验证码',
          },
        ],
      },
      loading: false,
      captcha: '',
    }
  },
  beforeDestroy() {
    this.timertimer = null
    clearInterval(this.timertimer)
  },
  mounted() {
    this.getCaptcha()
  },
  methods: {
    translateTitle,
    // getPhoneCode() {
    //   if (!isPhone(this.form.user_mobile)) {
    //     this.$refs['registerForm'].validateField('user_mobile')
    //     return
    //   }
    //   this.isGetPhone = true
    //   let n = 60
    //   this.timertimer = setInterval(() => {
    //     if (n > 0) {
    //       n--
    //       this.phoneCode = this.translateTitle('获取验证码 ') + n + 's'
    //     } else {
    //       clearInterval(this.timertimer)
    //       this.phoneCode = this.translateTitle('获取验证码')
    //       this.timertimer = null
    //       this.isGetPhone = false
    //     }
    //   }, 1000)
    // },
    async getCaptcha() {
      const { data } = await captcha()
      this.captcha = data.captcha_img
      this.form.captcha_id = data.captcha_id
    },
    handleRegister() {
      this.$refs['registerForm'].validate(async (valid) => {
        this.loading = true
        if (valid) {
          const { ret } = await register(this.form)
          if (ret == 200) {
            this.loading = false
            this.$message({
              message: '恭喜您注册成功！即将前往登录页面。',
              type: 'success',
            })
            setTimeout(() => {
              this.$router.push('/login')
            }, 1000)
          } else {
            this.getCaptcha()
          }
          this.loading = false
        }
        this.loading = false
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.register-container {
  height: 100vh;
  min-height: 700px;
  background: url('~@/assets/login_images/background.jpg') center center fixed
    no-repeat;
  background-size: cover;

  .register-form {
    position: relative;
    max-width: 100%;
    padding: 4.5vh;
    margin: calc((100vh - 590px) / 2) 5vw 5vw;
    overflow: hidden;
    background: url('~@/assets/login_images/login_form.png');
    background-size: 100% 100%;

    .title-tips {
      margin-top: 29px;
      font-size: 26px;
      font-weight: 400;
      color: $base-color-white;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .register-btn {
      display: inherit;
      width: 100%;
      height: 50px;
      margin-top: 5px;
      border: 0;
      font-size: 18px;
      &:hover {
        opacity: 0.9;
      }
    }
    .loginbot {
      margin-top: 20px;
      display: flex;
      justify-content: space-between;
    }
    .phone-code {
      position: absolute;
      top: 8px;
      right: 10px;
      width: 120px;
      height: 32px;
      font-size: 14px;
      color: #fff;
      cursor: pointer;
      user-select: none;
      border-radius: 3px;
    }
    .captcha {
      display: flex;
      justify-content: space-between;
      .el-input {
        width: calc(100% - 130px);
      }
      .captchaImg {
        cursor: pointer;
        width: 120px;
        height: 48px;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        overflow: hidden;
        border-radius: 4px;
        img {
          width: 120px;
          height: 48px;
          object-fit: cover;
        }
      }
    }
  }

  .tips {
    margin-bottom: 10px;
    font-size: $base-font-size-default;
    color: $base-color-white;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  :deep() {
    .el-form-item {
      padding-right: 0;
      margin: 20px 0;
      color: #454545;
      background: transparent;
      border: 1px solid transparent;
      border-radius: 2px;

      i {
        position: absolute;
        top: 8px;
        left: 5px;
        z-index: $base-z-index;
        font-size: 16px;
        color: #d7dee3;
        cursor: pointer;
        user-select: none;
      }

      &__content {
        min-height: $base-input-height;
        line-height: $base-input-height;
      }

      &__error {
        position: absolute;
        top: 100%;
        left: 18px;
        font-size: $base-font-size-small;
        line-height: 18px;
        color: $base-color-red;
      }
    }

    .el-input {
      box-sizing: border-box;

      input {
        height: 48px;
        padding-left: 35px;
        font-size: $base-font-size-default;
        line-height: 58px;
        background: #f6f4fc;
        border: 0;
      }
    }
  }
}
</style>
